﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iOak.Business.Campaigns;
using iOak.Business.Resumes;
using iOak.Domain.Campaigns;
using iOak.Domain.Resumes;
using System.Data;
using iOak.Common;
using iOak.Common.Untity;

namespace iOak.Web.ResumeTemplates
{
    public partial class CandidateList : UserControl
    {
        private static readonly ApplicationCandidateService itemApplicationCandidateService = new ApplicationCandidateService();
        private static readonly ApplicationService itemApplicationService = new ApplicationService();
        private static readonly ResumeDetailService itemResumeDetailService = new ResumeDetailService();

        private List<DynamicColumn> DataSource
        {
            get { return this.Session["CandidateList_DataSource"] as List<DynamicColumn>; }
            set { this.Session["CandidateList_DataSource"] = value; }
        }

        private ApplicationBase ApplicationItem 
        {
            get { return this.Session["CandidateList_ApplicationItem"] as ApplicationBase; }
            set { this.Session["CandidateList_ApplicationItem"] = value; }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            pagerControl.PageIndexChange += new Modules.PagerControl.OnPageIndexChangeHandler(pagerControl_PageIndexChange);
        }

        protected void pagerControl_PageIndexChange(object serder, EventArgs e)
        {
            pagerControl.BindData(gvCandidate, DataSource);
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="applicationId"></param>
        public void BindData(Guid applicationId)
        {
            ApplicationItem = itemApplicationService.Retrieve(applicationId);
            var list = new List<ApplicationCandidate>();
            list = itemApplicationCandidateService.GetListByApplicationId(applicationId);
            // 获取当前绑定的列表Key
            var bindList = new List<DynamicColumn>();

            list.ForEach(item =>
            {
                var addItem = new DynamicColumn();
                var listResumeDetail = itemResumeDetailService.GetListByResumeId(item.ResumeId);
                addItem.ResumeId = item.ResumeId;
                addItem.ApplicationCandidateId = item.ApplicationCandidateId;
                var tempValue = listResumeDetail.FirstOrDefault(m => m.Key.Equals("UserName"));
                if (tempValue != null)
                {
                    addItem.UserName = tempValue.ValueString;
                }
                tempValue = listResumeDetail.FirstOrDefault(m => m.Key.Equals("Gender"));
                if (tempValue != null)
                {
                    addItem.Gender = tempValue.ValueString;
                }
                tempValue = listResumeDetail.FirstOrDefault(m => m.Key.Equals("Professional"));
                if (tempValue != null)
                {
                    addItem.Professional = tempValue.ValueString;
                }
                tempValue = listResumeDetail.FirstOrDefault(m => m.Key.Equals("ReportingDate"));
                if (tempValue != null)
                {
                    addItem.UpdateOn = tempValue.ValueString;
                }
                tempValue = listResumeDetail.FirstOrDefault(m => m.Key.Equals("AcademicDegree"));
                if (tempValue != null) 
                { 
                    addItem.AcademicDegree =tempValue.ValueString;
                }
                tempValue = listResumeDetail.FirstOrDefault(m => m.Key.Equals("CountryStudy"));
                if (tempValue != null)
                {
                    addItem.CountryStudy = tempValue.ValueString;
                }
                tempValue = listResumeDetail.FirstOrDefault(m => m.Key.Equals("DateOfBirth"));
                if (tempValue != null)
                {
                    addItem.DateOfBirth = tempValue.ValueString;
                }
                tempValue = listResumeDetail.FirstOrDefault(m => m.Key.Equals("StudyInstitutes"));
                if (tempValue != null)
                {
                    addItem.StudyInstitutes = tempValue.ValueString;
                }
                bindList.Add(addItem);
            });
            DataSource = bindList;
            pagerControl.BindData(gvCandidate, DataSource);
        }

        protected void gvCandidate_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DynamicColumn dynamicColumn = e.Row.DataItem as DynamicColumn;
                Label lblPassed = e.Row.FindControl("lblPassed") as Label;
                HyperLink hlCheck = e.Row.FindControl("hlCheck") as HyperLink;
                ApplicationCandidate applicationCandidate = itemApplicationCandidateService.Retrieve(dynamicColumn.ApplicationCandidateId);
                lblPassed.Text = ResourceHelper.GetCandidateStatusName(applicationCandidate.Passed);
                //if (ApplicationItem.Status == (int)ApplicationStatus.Checking)
                if (ApplicationItem.Status == (int)ApplicationStatus.Submitted)
                {
                    hlCheck.NavigateUrl = String.Format("/Administration/ApplicationCheck.aspx?applicationId={0}&applicationCandidateId={1}",
                                                        applicationCandidate.ApplicationId.ToString(),
                                                        applicationCandidate.ApplicationCandidateId.ToString());
                }
                else 
                {
                    hlCheck.Visible = false;
                }
            }
        }
    }
}